import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_graphics_view/src/context/scene_state_context.dart';
import 'package:flutter_graphics_view/src/model/item_model_data.dart';
import 'package:flutter_graphics_view/src/policy/base/policy_set.dart';
import 'package:provider/provider.dart';

class GraphicsItem extends StatelessWidget {
  final PolicySet policy;

  const GraphicsItem({
    Key? key,
    required this.policy,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final itemModelData = Provider.of<ItemModelData>(context);
    final sceneStateContext = Provider.of<SceneStateContext>(context);

    return Positioned(
      left: sceneStateContext.scale * itemModelData.position.dx +
          sceneStateContext.position.dx,
      top: sceneStateContext.scale * itemModelData.position.dy +
          sceneStateContext.position.dy,
      width: sceneStateContext.scale * itemModelData.size.width,
      height: sceneStateContext.scale * itemModelData.size.height,
      child: Listener(
          onPointerSignal: (PointerSignalEvent event) {
            policy.onGraphicsItemPointerSignal(itemModelData.id, event);
          },
          child: GestureDetector(
            behavior: HitTestBehavior.translucent,
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                Positioned(
                  left: 0,
                  top: 0,
                  width: itemModelData.size.width,
                  height: itemModelData.size.height,
                  child: Container(
                    transform: Matrix4.identity()
                      ..scale(sceneStateContext.scale),
                    child: policy.showGraphicsItemBody(itemModelData),
                  ),
                ),
                policy.showCustomWidgetWithItemModelData(
                    context, itemModelData),
              ],
            ),
            onTap: () => policy.onGraphicsItemTap(itemModelData.id),
            onTapDown: (TapDownDetails details) =>
                policy.onGraphicsItemTapDown(itemModelData.id, details),
            onTapUp: (TapUpDetails details) =>
                policy.onGraphicsItemTapUp(itemModelData.id, details),
            onTapCancel: () => policy.onGraphicsItemTapCancel(itemModelData.id),
            onScaleStart: (ScaleStartDetails details) =>
                policy.onGraphicsItemScaleStart(itemModelData.id, details),
            onScaleUpdate: (ScaleUpdateDetails details) =>
                policy.onGraphicsItemScaleUpdate(itemModelData.id, details),
            onScaleEnd: (ScaleEndDetails details) =>
                policy.onGraphicsItemScaleEnd(itemModelData.id, details),
            onLongPress: () => policy.onGraphicsItemLongPress(itemModelData.id),
            onLongPressStart: (LongPressStartDetails details) =>
                policy.onGraphicsItemLongPressStart(itemModelData.id, details),
            onLongPressMoveUpdate: (LongPressMoveUpdateDetails details) =>
                policy.onGraphicsItemLongPressMoveUpdate(
                    itemModelData.id, details),
            onLongPressEnd: (LongPressEndDetails details) =>
                policy.onGraphicsItemLongPressEnd(itemModelData.id, details),
            onLongPressUp: () =>
                policy.onGraphicsItemLongPressUp(itemModelData.id),
          )),
    );
  }
}
